<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

// This page set the menu variables

// Go through all page checking for menu vars


$site_menus = array();
$link = construct_link(array("page","action"));


$r = $db->query( "SELECT * FROM menu" );
$list = array();
$list_en = array();
foreach($r as $key){
    $id = $key[id];
    if ($key[hidden] == 1){
        if ($admin == 0){
            continue;
        } else {
            $key[title] .= " ("._("hidden").")";
        }
    }

    if ( $key[lang] == "en_US" or $key[lang] == "en" ){
        //$list_en[] = array($key[title],$link2, );
        $list_en[$id] = $key[title];
        $menus_rank[$id] = $key[position];
    }
    elseif ( $key[lang] == $lang ) {
        //$list[] = array($key[title],$link2, $key[id]);

        $list[$id] = $key[title];

    }

}

natcasesort($menus_rank);


foreach($menus_rank as $id => $rank){
    $link2 = $link."page=$id";
    $title = html_entity_decode($list[$id]);
    if (!$title or $title == "" ) {

        $title = html_entity_decode($list_en[$id]);
        if ($main_site_gtrans == 'true'){
            $title = util_translate($title, "en", $lang);
        }
    }
    $site_menus[$id] = array($title, $link2);
}

unset($list);
unset($list_en);

// create list of levels
$menu_levels;

function menu_get_children($id){
    global $db, $lang;
    $children = array();
    $r = $db->query("SELECT * FROM menu WHERE ( lang = \"en\" OR lang = \"en_US\" ) ");
    foreach ($r as $key){

        if ($key[parent] == $id){
            $pos = $key[position];
            $children[] = array(
                    "id" => $key[id],
                    "rang" => $key[position],

                    );
        }
    }
    $children = array_sort($children, "rang");
    $children2 = array();
    foreach ($children as $a){
        $id = $a["id"];
        $children2[] = $id;
    }
    return $children2;
}

function menu_level($a, $level=-1){
    global $site_menus, $site_content, $menu_levels;
    $level ++;
    foreach ($a as $id){
        $menu_levels[$id] = $level;
        $a2 = menu_get_children($id);
        menu_level($a2, $level);
    }
}

function menu_get_parent($id){
    global $db;
    $r = $db->get_values_c("menu", "id = $id AND ( lang = \"en\" OR lang = \"en_US\" )");
    $parent_id = $r[parent];
    return $parent_id;
}

function menu_get_adam_eve($id){
    $parent_id = menu_get_parent($id);
    while ($parent_id > -1){
        $old_id = $parent_id;
        $parent_id = menu_get_parent($parent_id);
    }
    return $old_id;
}

$a = array();
foreach($site_menus as $id => $entry){
    $parent_id = menu_get_parent($id);
    if ( $parent_id == -1 ){
        $a[] = $id;
    }
}
menu_level($a);


function menu_get_item($id, $lang){
    global $site_menus, $db;
    $r = $db->get_values_c("menu", "id = $id AND lang = \"$lang\"");
    $title = $r[title];
    if (!$title){
        $r = $db->get_values_c("menu", "id = $id AND (lang = \"en\" OR lang = \"en_US\")");
        $title = $r[title];
        $translated = 0;
    }
    else {
        $translated = 1;
    }

    $parent = $r[parent];
    $rang = $r[position];
    $hidden = $r[hidden];
    //decode
    $title = html_entity_decode($title, ENT_QUOTES);

    return array($title, $parent, $rang, $translated, $hidden);
}

?>
